package com.dragon.mmochat.message;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.model.enumType.MmochatPetStateType;
import com.dragon.mmochat.model.role.MmochatMonster;
import com.dragon.mmochat.model.role.MmochatPet;
import com.dragon.mmochat.model.skill.MmochatSkill;
import com.dragon.mmochat.service.MmochatBattleService;
import com.dragon.mmochat.service.MmochatPetService;
import com.skymobi.buffer.SkymobiBuffer;
import com.skymobi.message.SkymobiProtocolMessage;

//增加或修改终端的宠物列表；删除宠物用另一协议
public class Mmochat_5_10S extends SkymobiProtocolMessage {
	private boolean reset = false; // false:更新; true:重置(先清空原来的宠物列表,再增加新宠物)
	private List<MmochatPet> pets = new ArrayList<MmochatPet>();

	public Mmochat_5_10S() {
		setMsgCode(MmochatConstant.MSGCODE_MMOCHAT_S_5_10);
	}

	@Override
	protected void encodeBody(SkymobiBuffer out) {
		if (reset) {
			out.put((byte) 1);
		} else {
			out.put((byte) 0);
		}
		if (pets.size() == 0) {
			out.put((byte) 0);
		} else {
			Collections.sort(pets);
			out.put((byte) pets.size());
			// TODO:排序
			for (MmochatPet pet : pets) {
				/*********** 基本信息 ***********/
				// 宠物id
				out.putInt(pet.getPetId());
				// 宠物名
				out.putPrefixedString(pet.getName(), 1);
				// 宠物类型
				if (pet.getState() == MmochatPetStateType.替补) {
					out.putPrefixedString(
							pet.getRoleType().toString() + "(替补)", 1);
				} else {
					out.putPrefixedString(pet.getPetTypeName(), 1);
				}
				// bmpId
				out.putUnsignedShort(pet.getBmpType().getValue());
				// 等级
				out.putInt((short) pet.getLevel());
				// 武学
				out.putInt(pet.getDao());
				// 状态
				out.put((byte) pet.getState().getValue());

				/*********** 属性 ***********/
				out.putInt(pet.getCurBlood());
				out.putInt(pet.getTotalBlood());
				out.putInt(pet.getCurSprit());
				out.putInt(pet.getTotalSprit());
				out.putInt(pet.getAttack_point());
				out.putInt(pet.getSprit_point());
				out.putInt(pet.getBlood_point());
				out.putInt(pet.getSpeed_point());
				out.putInt(pet.getPhysicalAttack());
				out.putInt(pet.getSpritAttack());
				out.putInt(pet.getPhysicalDefend());
				out.putInt(pet.getMagicDefend());
				out.putInt(pet.getSpeed());
				out.putInt(pet.getSpace_point());
				out.putInt(pet.getExp());
				out.putInt(Math.max(1, MmochatBattleService
						.getPetExpNeedToUplevel(pet.getLevel(), pet
								.getLife_turn())));
				/*********** 法术技能 ***********/
				out.put((byte) pet.getSkills().size());
				if (pet.getSkills().size() > 0) {
					List<MmochatSkill> skills = new ArrayList<MmochatSkill>(pet
							.getSkills().values());
					Collections.sort(skills);
					for (MmochatSkill skill : skills) {
						out.putPrefixedString(skill.getType().toString(), 1);
						out.put((byte) skill.getTargetNum(pet));
					}
				}
				/*********** 成长 ***********/
				out.putShort((short) (pet.getBlood_protential() * (1 + pet
						.getBloodExtraPercent())));
				out
						.putShort((short) (pet.getPhysicalAttack_protential() * (1 + pet
								.getPhysicalExtraPercent())));
				out
						.putShort((short) (pet.getMagicAttack_protential() * (1 + pet
								.getMagicExtraPercent())));
				out.putShort((short) (pet.getSpeed_protential() * (1 + pet
						.getSpeedExtraPercent())));
				out.putShort((short) pet.getPhysicalDefend_protential());
				out.putShort((short) pet.getMagicDefend_protential());
				// 此宠物最大成长
				MmochatMonster monster = MmochatPetService.monsterBaseAttrData
						.get(pet.getRoleType());
				out.putShort((short) (monster.getBlood_protential() * (1 + pet
						.getBloodExtraPercent())));
				out
						.putShort((short) (monster
								.getPhysicalAttack_protential() * (1 + pet
								.getPhysicalExtraPercent())));
				out
						.putShort((short) (monster.getMagicAttack_protential() * (1 + pet
								.getMagicExtraPercent())));
				out.putShort((short) (monster.getSpeed_protential() * (1 + pet
						.getSpeedExtraPercent())));
				out.putShort((short) monster.getPhysicalDefend_protential());
				out.putShort((short) monster.getMagicDefend_protential());
			}
		}
	}

	public void addPet(MmochatPet pet) {
		pets.add(pet);
	}

	public void setPets(List<MmochatPet> pets) {
		this.pets = pets;
	}

	public void setReset(boolean reset) {
		this.reset = reset;
	}

	public boolean hasPet() {
		return pets.size() > 0;
	}

}